package com.ft.admix.bd.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.baidu.mobads.sdk.api.SplashAd;
import com.baidu.mobads.sdk.api.SplashAdListener;
import com.baidu.mobads.sdk.api.SplashInteractionListener;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsSplashScreenAd;
import com.ft.admix.ad.MixCustomSplashAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.utils.ThreadUtils;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class SplashAdapterLoader extends MixCustomSplashAdapter {

    SplashAd mSplashAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
        mSplashAd = new SplashAd(context, config.getAdId(), new SplashAdListener() {
            @Override
            public void onADLoaded() {
                callLoadSuccess();

            }

            @Override
            public void onAdFailed(String s) {
                callLoadFailed(20001, s);
            }
        });
        mSplashAd.setListener(new SplashInteractionListener() {
            @Override
            public void onLpClosed() {

            }

            @Override
            public void onAdPresent() {

            }

            @Override
            public void onAdExposed() {
                callSplashAdShow();
            }

            @Override
            public void onAdDismissed() {
                callSplashAdDismiss();
            }

            @Override
            public void onAdSkip() {
                callSplashAdSkip();
            }

            @Override
            public void onAdClick() {
                callSplashAdClicked();
            }

            @Override
            public void onAdCacheSuccess() {

            }

            @Override
            public void onAdCacheFailed() {

            }

            @Override
            public void onADLoaded() {

            }

            @Override
            public void onAdFailed(String s) {

            }
        });
        mSplashAd.load();
    }

    @Override
    public void show(ViewGroup container) {
        if (mSplashAd != null) {
            mSplashAd.show(container);
        }
    }

    @Override
    public void destroy() {
        if (mSplashAd != null) {
            mSplashAd.destroy();
        }
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                if (mSplashAd != null && mSplashAd.isReady()) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }
} 